from marshmallow import fields, post_load

from gis.common.django_ext.forms import BaseSchema, EnumField, ExportSchema
from itpay.core.agency.const import AgencyType


class AgencyListSchema(ExportSchema):
    id = fields.Integer()
    agency_id = fields.Integer()
    agency_name = fields.String()
    enable = fields.Boolean()
    mch_id = fields.Integer()
    parent_id = fields.Integer()
    parent_name = fields.String()
    agency_type = EnumField(AgencyType)
    use_vcode = fields.Boolean()
    working = fields.Boolean()
    use_white_ips = fields.Boolean()
    level = fields.Integer()
    user_level = fields.Integer()


class AddAgencySchema(BaseSchema):
    user_name = fields.String(required=True)
    enable = fields.Boolean(required=True)
    use_vcode = fields.Boolean()
    mch_ids = fields.List(fields.Integer(), allow_none=True)
    remark = fields.String(allow_none=True)
    white_ips = fields.List(fields.String(), allow_none=True)
    avatar = fields.String(allow_none=True)

    @post_load
    def post_load_func(self, data):
        data["user_name"] = data["user_name"].strip()
        return data


class UpdateAgencySchema(BaseSchema):
    enable = fields.Boolean(required=True)
    use_vcode = fields.Boolean(allow_none=True)
    mch_ids = fields.List(fields.Integer(), allow_none=True)
    remark = fields.String(allow_none=True)
    white_ips = fields.List(fields.String(), allow_none=True)
    avatar = fields.String(allow_none=True)


class ComplainAgencySchema(ExportSchema):
    mch_id = fields.Integer()
    user_id = fields.Integer()
    agency_id = fields.Integer()
    agency_name = fields.String()
    parent_id = fields.Integer()
    parent_name = fields.String()
    complaint_content = fields.String()
    complaint_types = fields.Integer()
    created_at_begin = fields.DateTime()
    created_at_end = fields.DateTime()
    updated_at_begin = fields.DateTime()
    updated_at_end = fields.DateTime()
    has_done = fields.Boolean()


class HandleComplainSchema(BaseSchema):
    remark = fields.String(missing="")
